//
//  DSLoginViewController.m
//  外星球生活圈
//
//  Created by MR.ZHOU on 15/8/13.
//  Copyright (c) 2015年 MR.ZHOU. All rights reserved.
//

#import "ZFLoginViewController.h"
#import "ZFLoginCollectionViewCell.h"
#import "ZFUserInfo.h"
#import "SSKeychain.h"
#import <WeiboSDK.h>

@interface ZFLoginViewController ()<UICollectionViewDelegate,UICollectionViewDataSource>
@property (weak, nonatomic) IBOutlet UICollectionViewFlowLayout *collectionViewFlowLayout;

@property (weak, nonatomic) IBOutlet UICollectionView *collectionView;
@property(nonatomic,strong)NSMutableArray *thirdArray;
@property (weak, nonatomic) IBOutlet UIView *userCiew;
@property (weak, nonatomic) IBOutlet UIView *pwdView;
@property (weak, nonatomic) IBOutlet UITextField *userNameField;

@property (weak, nonatomic) IBOutlet UITextField *passwordFiled;
- (IBAction)rmPwdBtn;
@property (weak, nonatomic) IBOutlet UIButton *ftPwdBtn;

@property (weak, nonatomic) IBOutlet UIButton *login;
@property (weak, nonatomic) IBOutlet UIButton *zhuCe;
@property (weak, nonatomic) IBOutlet UILabel *thirdLg;
@end
#define DSLoginColor [UIColor colorWithRed:114/255.0 green:198/255.0 blue:252/255.0 alpha:1.0]
@implementation ZFLoginViewController


- (void)viewDidLoad {
    [super viewDidLoad];
    
    _userCiew.backgroundColor =DSLoginColor;
    _pwdView.backgroundColor = DSLoginColor;
    
    _login.backgroundColor = DSLoginColor;
    [_login setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    
    _collectionViewFlowLayout.itemSize = CGSizeMake(_collectionView.bounds.size.width/4, _collectionView.bounds.size.height);
    _collectionViewFlowLayout.minimumInteritemSpacing = 0;
    
    _collectionViewFlowLayout.minimumLineSpacing = 0;
    
  
    
    
    
}
-(void)awakeFromNib{
    _thirdArray = [NSMutableArray array];
    [_thirdArray addObject:@{@"name":@"新浪",@"icon":@"login_wb"}];
    [_thirdArray addObject:@{@"name":@"QQ",@"icon":@"login_qq"}];
    [_thirdArray addObject:@{@"name":@"微信",@"icon":@"login_wx"}];
    [_thirdArray addObject:@{@"name":@"豆瓣",@"icon":@"login_db"}];
    
}
- (IBAction)loginBtnClick:(id)sender {
    //获取bundle ID
    NSString *service = [NSBundle mainBundle].bundleIdentifier;
    
  [SSKeychain setPassword:_passwordFiled.text forService:service account:_userNameField.text];
 
    //用于存放参数
    NSMutableDictionary *param  =[NSMutableDictionary dictionary];
    
    [param setObject:_passwordFiled.text forKey:@"password"];
    
    [param setObject:_userNameField.text forKey:@"curr_account"];
    //时间戳
    [param setObject:[DSFunction getTimeScamp] forKey:@"time"];
    //MD5
    NSArray *array = @[ APP_ID,param[@"password"],param[@"time"],APP_IDENGER];
    
    [param setObject:[DSFunction md5StringFromArray:array] forKey:@"sign"];
    
    [param setObject:APP_ID forKey:@"app_id"];
    #define LoginUrl @"index.php?anu=api/1/user/dispose_login"
    
    [DSHTTPClient DSPOST:LoginUrl withParam:param withSuccessBlock:^(id data) {
        
        [ZFUserInfo InfoWithDict:data];
        
        //
        [DSFunction saveUserInfo:data];
        
        [self backMethod];
        
        DSLog(@"%@",data);
        
    } withFailedBlock:^(NSError *error) {
        DSLog(@"%@",error);
    } withErrorBlock:^(id error) {
        DSLog(@"%@",error);
    }];
    
    
}
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
    [self.view endEditing:YES];
}
- (IBAction)fgBtn {
}

- (IBAction)rmPwdBtn {
}

-(void)backMethod{
    [self.navigationController popToRootViewControllerAnimated:YES];
    
    AppDelegate *app = (AppDelegate *)[UIApplication sharedApplication].delegate;
    
    [app.tab pushHidden:NO];
  
}
#pragma mark  UICollectionViewDataSorce

-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return _thirdArray.count;
    
}
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    ZFLoginCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"DSLoginCollectionViewCell" forIndexPath:indexPath];
    
    cell.dict = _thirdArray[indexPath.row];
    
    return cell;
}

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    WBAuthorizeRequest *request = [WBAuthorizeRequest request];
    request.redirectURI = @"http://www.baidu.com";
    request.scope = @"all";
    request.userInfo = @{@"SSO_From": @"SendMessageToWeiboViewController",
                         @"Other_Info_1": [NSNumber numberWithInt:123],
                         @"Other_Info_2": @[@"obj1", @"obj2"],
                         @"Other_Info_3": @{@"key1": @"obj1", @"key2": @"obj2"}};
    [WeiboSDK sendRequest:request];
}
@end
